package com.tdcoding.chat.manager;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.tdcoding.chat.configure.Configuration;
import com.tdcoding.chat.helper.Logger;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * 缓存管理类，针对不同业务会有多个连接池,单例
 * 
 * @author Wellsa
 */
public class RedisManager {
	private static final String TAG = "RedisManager";
	private static ShardedJedisPool jedisPool;

	public static void initRedisManager(Configuration configuration) throws IOException {
		Document document = configuration.configRedis();
//		Logger.println(TAG, "initRedisManager", document);
		if (document == null) {
			return;
		}
		Element maxIdle = document.getElementsByTag("maxIdle").first();
		Element maxWait = document.getElementsByTag("maxWait").first();
		Element maxTotal = document.getElementsByTag("maxTotal").first();
		Element jmxEnabled = document.getElementsByTag("jmxEnabled").first();
//		Element testOnReturn = document.getElementsByTag("testOnReturn").first();
//		Element testWhileIdle = document.getElementsByTag("testWhileIdle").first();
		Element testOnBorrow = document.getElementsByTag("testOnBorrow").first();
		Element connectionTimeout = document.getElementsByTag("connectionTimeout").first();
//		Element numTestsPerEvictionRun = document.getElementsByTag("numTestsPerEvictionRun").first();
//		Element minEvictableIdleTimeMillis = document.getElementsByTag("minEvictableIdleTimeMillis").first();
//		Element timeBetweenEvictionRunsMillis = document.getElementsByTag("timeBetweenEvictionRunsMillis").first();
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		// 设置池配置项值
		if (maxTotal != null && maxTotal.hasText()) {
			int value = Integer.valueOf(maxTotal.text());
			poolConfig.setMaxTotal(value);
		}
		if (jmxEnabled != null && jmxEnabled.hasText()) {
			boolean value = Boolean.parseBoolean(jmxEnabled.text());
			poolConfig.setJmxEnabled(value);
		}
		if (maxWait != null && maxWait.hasText()) {
			int value = Integer.valueOf(maxWait.text());
			poolConfig.setMaxWaitMillis(value);
		}
		if (maxIdle != null && maxIdle.hasText()) {
			int value = Integer.valueOf(maxIdle.text());
			poolConfig.setMaxIdle(value);
		}

		if (testOnBorrow != null && testOnBorrow.hasText()) {
			boolean value = Boolean.parseBoolean(testOnBorrow.text());
			poolConfig.setTestOnBorrow(value);
		}
//		if (testOnReturn != null && testOnReturn.hasText()) {
//			boolean value = Boolean.parseBoolean(testOnReturn.text());
//			poolConfig.setTestOnReturn(value);
//		}
//		if (testWhileIdle != null && testWhileIdle.hasText()) {
//			boolean value = Boolean.parseBoolean(testWhileIdle.text());
//			poolConfig.setTestWhileIdle(value);
//		}

//		if (numTestsPerEvictionRun != null && numTestsPerEvictionRun.hasText()) {
//			int value = Integer.parseInt(numTestsPerEvictionRun.text());
//			poolConfig.setNumTestsPerEvictionRun(value);
//		}
//		if (minEvictableIdleTimeMillis != null && minEvictableIdleTimeMillis.hasText()) {
//			int value = Integer.parseInt(minEvictableIdleTimeMillis.text());
//			poolConfig.setTimeBetweenEvictionRunsMillis(value);
//		}
//		if (timeBetweenEvictionRunsMillis != null && timeBetweenEvictionRunsMillis.hasText()) {
//			int value = Integer.parseInt(timeBetweenEvictionRunsMillis.text());
//			poolConfig.setMinEvictableIdleTimeMillis(value);
//		}

		int iconnectionTimeout = Integer.parseInt(connectionTimeout.text());
		List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
		Elements jedisNodes = document.getElementsByTag("jedis");
		for (Element element : jedisNodes) {
			String host = element.getElementsByTag("host").first().text();
			String pwd = element.getElementsByTag("pwd").first().text();
			String name = element.getElementsByTag("name").first().text();
			int port = Integer.parseInt(element.getElementsByTag("port").first().text());
			JedisShardInfo shardInfo = new JedisShardInfo(host, port, name);
			shardInfo.setConnectionTimeout(iconnectionTimeout);
			shardInfo.setPassword(pwd);
			Logger.println(TAG, "for", host,pwd,name,port,iconnectionTimeout);
			jedisShardInfos.add(shardInfo);
		}
		jedisPool = new ShardedJedisPool(poolConfig, jedisShardInfos);
	}

	/**
	 * 增加计数
	 * 
	 * @param key
	 * @return
	 */
	public static long incrCount(String key) {
		long result =-1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.incr(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 减少计数
	 * 
	 * @param key
	 * @return
	 */
	public static long decrCount(String key) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.decr(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 存字符串
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static long addToString(String key, String value) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.setnx(key, value);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 存字符串 并设置过期时间
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static String addToString(String key, String value, int seconds) {
		String result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.setex(key, seconds, value);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 添加名称为key的string的值附加value
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static long appendToString(String key, String value) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.append(key, value);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 截取为key的string的value的子串
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static String subString(String key, int start, int end) {
		String result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.substr(key, start, end);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 存集合
	 * 
	 
	 * @param key
	 * @param value
	 * @return
	 */
	public static long addToSet(String key, String... members) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sadd(key, members);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 存列表到首/尾
	 * 
	 * @param key
	 * @param value
	 * @param addToHead
	 * @return
	 */
	public static long addToList(String key, boolean addToHead, String... values) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			if (addToHead)
				result = jedis.lpush(key, values);
			else
				result = jedis.rpush(key, values);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}
	
	/**
	 * 存列表到首/尾
	 * 
	 * @param key
	 * @param value
	 * @param addToHead
	 * @return
	 */
	public static long addToList( byte[] key, boolean addToHead, byte[]... values) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			if (addToHead)
				result = jedis.lpushx(key, values);
			else
				result = jedis.rpush(key, values);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 存Map(多个)
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static String addToMap(String key, HashMap<String, String> mValues) {
		String result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hmset(key, mValues);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 存Map（单个）
	 * 
	 * @param key
	 * @param field
	 * @param value
	 * @return
	 */
	public static long addToMap(String key, String field, String value) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hset(key, field, value);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}
	
	/**
	 * 存Map（单个）
	 * 
	 * @param key
	 * @param field
	 * @param value
	 * @return
	 */
	public static long addToMap(byte[] key, byte[] field, byte[] value) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hset(key, field, value);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取String值
	 * 
	 * @param key
	 * @return
	 */
	public static String getStringBykey(String key) {
		String result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.get(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取Set的长度
	 * 
	 * @param key
	 * @return
	 */
	public static long getSetCount(String key) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.scard(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取所有Set值
	 * 
	 * @param key
	 * @return
	 */
	public static Set<String> getSetByKey(String key) {
		Set<String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.smembers(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 返回名称为key的zset（元素已按score从大到小排序）中的index从start到end的所有元素
	 * 
	 * @param key
	 * @param pageIndex
	 * @param pageCount
	 * @return
	 */
	public static Set<String> getSetByDesc(String key, int pageIndex, int pageCount) {
		int start = pageCount * (pageIndex - 1);// 因为redis中list元素位置基数是0
		int end = start + pageCount - 1;
		Set<String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.zrevrange(key, start, end);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 返回名称为key的zset（元素已按score从小到大排序）中的index从start到end的所有元素
	 * 
	 * @param key
	 * @param pageIndex
	 * @param pageCount
	 * @return
	 */
	public static Set<String> getSetByAsc(String key, int pageIndex, int pageCount) {
		int start = pageCount * (pageIndex - 1);// 因为redis中list元素位置基数是0
		int end = start + pageCount - 1;
		Set<String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.zrange(key, start, end);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取所有List值
	 * 
	 * @param key
	 * @return
	 */
	public static List<String> getListByKey(String key) {
		List<String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lrange(key, 0, -1);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取指定下标List值
	 * 
	 * @param key
	 * @param index
	 * @return
	 */
	public static String getListByKey(String key, int index) {
		String result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lindex(key, index);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 根据区间获取List值
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public static List<String> getListByKey(String key, int pageIndex, int pageCount) {
		int start = pageCount * (pageIndex - 1);// 因为redis中list元素位置基数是0
		int end = start + pageCount - 1;
		List<String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lrange(key, start, end);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取名称为key的zset的基数
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public static long getListByBase(String key) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.zcard(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取所有Map的键值
	 * 
	 * @param key
	 * @return
	 */
	public static String getMapByKey(String key, String field) {
		String result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hget(key, field);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}
	
	/**
	 * 获取所有Map的键值
	 * 
	 * @param key
	 * @return
	 */
	public static byte[] getMapByKey(byte[] key, byte[] field) {
		byte[]  result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hget(key, field);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取所有Map的键值
	 * 
	 * @param key
	 * @return
	 */
	public static HashMap<String, String> getMapByKey(String key) {
		HashMap<String, String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = (HashMap<String, String>) jedis.hgetAll(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取Map指定名称的键值
	 * 
	 * @param key
	 * @param fields
	 * @return
	 */
	public static List<String> getMapByKey(String key, String... fields) {
		List<String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hmget(key, fields);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取Map指定Key的所有值
	 * 
	 * @param key
	 * @return
	 */
	public static List<String> getMaplistByKey(String key) {
		List<String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hvals(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取名称为key的hash中所有键
	 * 
	 * @param key
	 * @return
	 */
	public static Set<String> getMapkeysByKey(String key) {
		Set<String> result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hkeys(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获取Map的长度
	 * 
	 * @param key
	 * @return
	 */
	public static long getMapCount(String key) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hlen(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}
	
	/**
	 * 获取Map的长度
	 * 
	 * @param key
	 * @return
	 */
	public static long getMapCount(byte[] key) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hlen(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 判断是否存在key
	 * 
	 * @param key
	 * @return
	 */
	public static boolean existKey(String key) {
		boolean result = false;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.exists(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 判断Set是否存在value
	 * 
	 * @param key
	 * @return
	 */
	public static boolean existInSet(String key, String column) {
		boolean result = false;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sismember(key, column);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 判断Map是否存在元素
	 * 
	 
	 * @param key
	 * @return
	 */
	public static boolean existInMap(String key, String field) {
		boolean result = false;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hexists(key, field);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 删除各类型key
	 * 
	 * @param key
	 */
	public static long deleteKey(String key) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.del(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 删除Map指定元素
	 * 
	 * @param key
	 * @param fields
	 */
	public static long delMapByKey(String key, String... fields) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hdel(key, fields);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}
	

	/**
	 * 删除Map指定元素
	 * 
	 * @param key
	 * @param fields
	 */
	public static long delMapByKey(byte[] key, byte[]... fields) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.hdel(key, fields);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 删除Set指定元素
	 * 
	 * @param key
	 * @param fields
	 */
	public static long delSetByKey(String key, String... fields) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.srem(key, fields);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 删除名称为key的zset中的元素member
	 * 
	 * @param key
	 * @param member
	 */
	public static long delSetByKey(String key, String member) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.zrem(key, member);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 删除List子集
	 * 
	 * @param count
	 * @param value
	 */
	public static long delListByKey(String key, int count, String value) {
		long result = -1;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lrem(key, count, value);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 删除List的首/尾
	 * 
	 * @param key
	 * @param head
	 */
	public static String delListByKey(String key, boolean head) {
		String result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			if (head)
				result = jedis.lpop(key);
			else
				result = jedis.rpop(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	// /**
	// * 重定位list的元素位置
	// 
	// * @param key
	// * @param head
	// */
	// public static String relocationList(String srckey,String dstkey) {
	// String result=null;
	// Jedis jedis = null;
	// try {
	// jedis = jedisPool.getResource();
	// result=jedis.rpoplpush(srckey,dstkey);
	// } catch (Exception e) {
	// // 释放redis对象
	//
	// e.printStackTrace();
	// } finally {
	// // 返还到连接池
	// jedisPool.returnResourceObject(jedis);
	// }
	// return result;
	// }

	/**
	 * 更新List子集
	 * 
	 * @param key
	 * @param newValue
	 * @param index
	 * @return
	 */
	public static String updateList(String key, String newValue, int index) {
		String result = null;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.lset(key, index, newValue);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 设置过期时间在多少秒以后
	 * 
	 * @param key
	 * @param seconds
	 * @return
	 */
	public static long expireKey(String key, int seconds) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.expire(key, seconds);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 设置过期时间在某个时间
	 * 
	 * @param key
	 * @param date
	 * @return
	 */
	public static long expireKey(String key, long date) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.expireAt(key, date);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 获得一个key的活动时间
	 * 
	 * @param key
	 * @return
	 */
	public static long getKeyTimeLive(String key) {
		long result = 0;
		ShardedJedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.ttl(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		} finally {
			// 返还到连接池
			jedisPool.returnResourceObject(jedis);
		}
		return result;
	}

	/**
	 * 断开ShardedJedisPool
	 * 
	 * @param jedisPool
	 */
	public static void destroy(ShardedJedisPool jedisPool) {
		if (jedisPool == null) {
			return;
		}
		jedisPool.destroy();
	}

}
